//
//  NYSubjectAnswerCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/4.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYSubjectAnswerCell.h"
@interface NYSubjectAnswerCell ()
@property(nonatomic, strong) QMUIGhostButton *optionBtn;
@property(nonatomic, strong) DTAttributedTextContentView *anwserLabel;
@property(nonatomic, strong) UIImageView *anwserImgView;
@property(nonatomic, strong) NYHtmlHelper *htmlHelper;
@property(nonatomic, strong) UIView *bottomContentView;
@end
@implementation NYSubjectAnswerCell
- (BOOL)shouldUpdateCellWithObject:(NICellObject *)object atIndexPath:(NSIndexPath *)indexPath
{
        self.indexPath = indexPath;
        self.subjectModel = (NYSubjectModel *)object.userInfo;
        self.subjectModel.indexPath = indexPath;
        return YES;
}
- (void)setSubjectModel:(NYSubjectModel *)subjectModel
{
        _subjectModel = subjectModel;
        [self updateContent];
}
- (void)updateContent
{
        BOOL isImg = (self.subjectModel.exercises_answer_type == NWQuestionAnswerContentTypeImg);
        self.anwserImgView.hidden = !isImg;
        self.anwserLabel.hidden = isImg;
        [self.optionBtn setTitle:[self getLetterFromIndex:self.indexPath.row - 1] forState:(UIControlStateNormal)];
        @weakify(self);
        if (isImg) {
                [self.anwserImgView sd_setImageWithURL:[NSURL URLWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.subjectModel.exercises_answer_arr[self.indexPath.row - 1])]];
        } else {
                
                self.anwserLabel.attributedString = [self.htmlHelper attributedStringForHtmlString:self.subjectModel.exercises_answer_arr[self.indexPath.row - 1] maxImageSize:CGSizeMake(self.contentView.width - __WGWidth(30), CGFLOAT_MAX) imgSizeCallBackBlock:^(CGSize imgSize) {
                        
                        @strongify(self);
                        if (self.subjectModel.attributeViewHeight <= 1) { /// 防止每次取 cell 的时候，闪屏卡顿
                                [self.parentTableView updateWithBlock:^(UITableView * _Nonnull tableView) {
                                        self.subjectModel.attributeViewHeight = [self.anwserLabel intrinsicContentSize].height;
                                        [self.anwserLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                                                make.top.left.right.bottom.equalTo(self.bottomContentView);
                                                make.height.mas_greaterThanOrEqualTo(self.subjectModel.attributeViewHeight);
                                        }];
                                }];
                        }
                }];
        }
}
- (void)setupView
{
        [super setupView];
        
        UIView *contentView = [[UIView alloc] init];
        [self.contentView addSubview:contentView];
        self.bottomContentView = contentView;
        [contentView addSubview:self.optionBtn];
        [contentView addSubview:self.anwserLabel];
        [contentView addSubview:self.anwserImgView];
        
        [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).mas_offset(__WGHeight(8));
                make.left.equalTo(self.contentView).mas_offset(__WGHeight(16));
                make.bottom.equalTo(self.contentView).mas_offset(-__WGHeight(8)).priorityLow();
                make.right.equalTo(self.contentView).mas_offset(-__WGHeight(16));
        }];
        
        [self.optionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.top.equalTo(contentView);
                make.height.width.mas_equalTo(__WGHeight(21));
                make.bottom.lessThanOrEqualTo(contentView);
        }];
        
        [self.anwserLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.optionBtn.mas_right).offset(__WGWidth(8));
                make.top.equalTo(self.optionBtn.mas_top);
                make.bottom.lessThanOrEqualTo(contentView);
                make.right.equalTo(contentView);
                make.height.mas_greaterThanOrEqualTo(self.subjectModel.attributeViewHeight == 0 ? 21 : self.subjectModel.attributeViewHeight);
        }];
        
        [self.anwserImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.optionBtn.mas_right).offset(__WGWidth(8));
                make.top.equalTo(self.optionBtn.mas_top);
                make.bottom.lessThanOrEqualTo(contentView);
                make.right.lessThanOrEqualTo(contentView);
                make.height.mas_greaterThanOrEqualTo(21);
        }];
}
- (NSString *)getLetterFromIndex:(NSUInteger)index
{
        /// 65 A
        return [NSString stringWithFormat:@"%c",(char)(index + 65)].uppercaseString;
}

/// MARK: - Lazy
- (QMUIGhostButton *)optionBtn
{
        if (_optionBtn == nil) {
                _optionBtn = [[QMUIGhostButton alloc] init];
                _optionBtn.ghostColor = UIColorHex(0x5396F7);
                _optionBtn.titleLabel.font = WGSystem13Font;
        }
        return _optionBtn;
}
- (DTAttributedTextContentView *)anwserLabel
{
        if (_anwserLabel == nil) {
                _anwserLabel = [self.htmlHelper createAttributedContentView];
        }
        return _anwserLabel;
}
- (UIImageView *)anwserImgView
{
        if (_anwserImgView == nil) {
                _anwserImgView = [[UIImageView alloc] init];
//                _anwserImgView.contentMode = UIViewContentModeScaleAspectFit;
        }
        return _anwserImgView;
}
- (NYHtmlHelper *)htmlHelper
{
        if (_htmlHelper == nil) {
                _htmlHelper = [[NYHtmlHelper alloc] init];
        }
        return _htmlHelper;
}
@end
